
import javax.xml.transform.TransformerException;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.PosixParser;
import org.apache.log4j.Logger;
import com.rabbitmq.client.Channel;

import com.rabbitmq.client.QueueingConsumer;
import com.herohead.*;

/*
 * Added command line parameter for rabbitmq server 
 */

public class m_getGMail {

	/**
	 * @param args
	 * @throws TransformerException 
	 */

	private static Options createOptions() {
		Options options = new Options();
		options.addOption("h", "help", false, "print this message and exit");
		options.addOption("u", "username", true, "GMail username");
		options.addOption("p", "password", true, "GMail password");
		options.addOption("s", "server", true, "RabbitMQ Server");
		return options;
	}

	private static void showHelp(Options options) {
		HelpFormatter h = new HelpFormatter();
		h.printHelp("help", options);
		System.exit(-1);
	}


	private static org.apache.log4j.Logger log= Logger.getLogger(m_getGMail.class);
	static Channel ch;
	static Channel channel;


	public static void main(String[] args) throws TransformerException, Exception {
		// TODO Auto-generated method stub

		String username = null;
		String password = null;
		String rabbitserver = null;

		Options options = createOptions();
		try {
			CommandLineParser parser = new PosixParser();
			CommandLine cmd = parser.parse(options, args);
			if (cmd.hasOption("h")) {
				showHelp(options);
			}     


			if (cmd.hasOption("h")) showHelp(options);	  
			if (cmd.hasOption("u")) {
				username = cmd.getOptionValue("u"); 
			} else {
				showHelp(options);
			}
			if (cmd.hasOption("p")) {
				password = cmd.getOptionValue("p"); 
			} else {
				showHelp(options);
			}	    
			if (cmd.hasOption("s")) {
				rabbitserver = cmd.getOptionValue("s"); 
			} else {
				showHelp(options);
			}	




		} catch (Exception e) {
			e.printStackTrace();
			showHelp(options);
		}

		Misc m = new Misc();

		log.info("Program started");
		log.debug("Java version = " + m.getSystemInfoJavaVersion());
		log.debug("SystemHostName = " + m.getSystemLocalHostName());
		Xml x = new Xml();
		String queue = "email";


		Rabbitmq rmq = new Rabbitmq();
		rmq.setHostname(rabbitserver);
		rmq.setLogin("backup", "networker", "backup");
		log.info("Connecting to " + rabbitserver);
		rmq.setupChannel();
		rmq.setQueue(queue);

		rmq.declareQueDurable();
		channel = rmq.getChannel();
		QueueingConsumer consumer = new QueueingConsumer(channel);

		channel.basicConsume(rmq.getQueue(), true, consumer);

		channel = rmq.getChannel();
		System.out.println("Waiting for message");
		while (true) {
			QueueingConsumer.Delivery delivery = consumer.nextDelivery();
			String message = new String(delivery.getBody());
			x.readEmail(message);

			SendGmail em = new SendGmail();

			em.setUserNamePassword(username, password);
			em.setToemail(x.getToEmail());
			em.setFromemail(x.getFromEmail());
			em.setSubject(x.getSubject());
			em.setBody(x.getBody());

			@SuppressWarnings("unused")
			Boolean good = SendGmail.sendEMail();

			log.info(x.getSmtp() + " ToMail = " + x.getToEmail() + " FromEmail = " + x.getFromEmail() + " Subject =" + x.getSubject() + " Body = " + x.getBody());
			channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
		}


	}

}
